﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using uMES.LeanManufacturing.Common;
using uMES.LeanManufacturing.ParameterDTO;
using uMES.LeanManufacturing.ReportBusiness;

public partial class Report_BwSynergicInfoReportForm : PageBase
{
    int m_PageSize = 50;
    uMESReportBusiness reportBal = new uMESReportBusiness();
    protected void Page_Load(object sender, EventArgs e)
    {
        uMESMasterPage master = this.Master as uMESMasterPage;
        master.strNavigation = "当前位置：工序外协报表";
        master.strTitle = "工序外协报表";
        master.ChangeFrame(true);
        upageTurning.PageIndexChanged += new pageTurning.PageIndexChangedEventHandler(() => { SearchData(upageTurning.CurrentPageIndex.ToString()); });
        WebPanel = WebAsyncRefreshPanel1;
        ClearMessage();
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {

        try {gdResult.Rows.Clear(); SearchData("1"); } catch (Exception ex) { DisplayMessage(ex.Message, false); }
    }
    protected void btnReSet_Click(object sender, EventArgs e)
    {
        txtContainerName.Text = "";txtProcessNo.Text = "";txtSpecName.Text = "";gdResult.Rows.Clear();
    }
    protected void btnExport_Click(object sender, EventArgs e)
    {
        try { ExportToExcel(); } catch (Exception ex) { DisplayMessage(ex.Message, false); }
    }
    void SearchData(string index)
    {
        Dictionary<string, string> para = new Dictionary<string, string>();
        para["CurrentPageIndex"] = index.ToString();
        para["PageSize"] = m_PageSize.ToString();

        para["ProcessNo"] = txtProcessNo.Text.Trim();
        para["ContainerName"] = txtContainerName.Text.Trim();
        para["StepName"] = txtSpecName.Text.Trim();
        

        uMESPagingDataDTO result = reportBal.GetSynergicDealInfo(para);

        this.gdResult.DataSource = result.DBTable;
        this.gdResult.DataBind();
            //给分页控件赋值，用于分页控件信息显示
            this.upageTurning.TotalRowCount = int.Parse(result.RowCount);
            this.upageTurning.RowCountByPage = m_PageSize;
       
    }

    DataTable SearchAllData( )
    {
        Dictionary<string, string> para = new Dictionary<string, string>();
        para["CurrentPageIndex"] ="all";
        para["PageSize"] = m_PageSize.ToString();

        para["ProcessNo"] = txtProcessNo.Text.Trim();
        para["ContainerName"] = txtContainerName.Text.Trim();
        para["StepName"] = txtSpecName.Text.Trim();


        uMESPagingDataDTO result = reportBal.GetSynergicDealInfo(para);

        return result.DBTable;

    }

    void ExportToExcel() {
        
        // DataTable dt = uMESCommonFunction.GetGridData(gdResult);

        DataTable dt = SearchAllData();
        //取有效列，且重命名列名
        DataView view = dt.DefaultView;

        dt = view.ToTable(false, "ProcessNo", "ContainerName", "ProductDesc", "workflowstepname", "Qty", "SynergicCompanyName", "ProposerName", "preposerdate", "CurrentStatus", "CurrentDealName", 
            "CurrentDealDate", "PlannedComplitionDate", "ConPlannedStartDate", "ConPlannedCompletionDate");

        dt.Columns["ProcessNo"].ColumnName = "工作令号";
        dt.Columns["ContainerName"].ColumnName = "批次";
        dt.Columns["ProductDesc"].ColumnName = "产品名称";
        dt.Columns["workflowstepname"].ColumnName = "工序";
        dt.Columns["Qty"].ColumnName = "数量";
        dt.Columns["SynergicCompanyName"].ColumnName = "外协单位";
        dt.Columns["ProposerName"].ColumnName = "申请人";
        dt.Columns["preposerdate"].ColumnName = "申请时间";
        dt.Columns["CurrentStatus"].ColumnName = "当前节点";
        dt.Columns["CurrentDealName"].ColumnName = "上一节点处理人";
        dt.Columns["CurrentDealDate"].ColumnName = "上节点处理时间";
        dt.Columns["PlannedComplitionDate"].ColumnName = "要求完成时间";
        dt.Columns["ConPlannedStartDate"].ColumnName = "计划开始日期";
        dt.Columns["ConPlannedCompletionDate"].ColumnName = "计划完成日期";

        #region 测试图片
       // var temp = new List<object>() {
       //                         System.Drawing.Image.FromFile( @"C:\MESShopfloor\MESShopfloor\images\ImageTemp\ImageTemp-2020_05_15_08_37_30_042.png"),
       //                         "财图示sfsdgff",System.Drawing.Image.FromFile( @"C:\MESShopfloor\MESShopfloor\images\ImageTemp\ImageTemp-2020_05_15_08_37_30_042.png"),
       //                         System.Drawing.Image.FromFile(@"C:\MESShopfloor\MESShopfloor\images\ImageTemp\ImageTemp-2020_05_15_08_49_06_415.png"),
       //                         "asdzwe似懂非懂as","asdzwe似懂非懂as好几个打野四块钱欧路词典发那么支持下阿萨德QWERTYUI欧派了解客户南方工程部"
       //                        };

       //var newPigPath= ExportHelper.MerageImagsAndStrs(temp);

       // foreach (DataRow dr in dt.Rows) {
       //     dr[1] = newPigPath;
       // }

        #endregion

        string strPath =Request.MapPath(Request.ApplicationPath)  ;       
      string filePath = ExportHelper.DataTableExportToExcel2(dt, "工序外协记录报表", strPath,new List<int>() { });
        string url = Request.Url.ToString();
       string baseUrl= Regex.Split(url,"/Report")[0];
        string fileUrl = baseUrl + filePath;


        string strScript = "<script>window.open('" + fileUrl + "');</script>";

        ExcuteScript(strScript);
    }
}